home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1995 Fall / PD-ROM F95.toast / Programming / Programming Utilities / String Magic 1.0 ƒ / Docs next >
Encoding:
Text File  |  1991-04-24  |  12.3 KB  |  352 lines  |  [TEXT/ttxt]

  1.                                                  24 Apr 91
  2.  
  3. String Magic - V1.0
  4. String Resource Maintenance Application
  5. © 1991 By Curt Sellmer 
  6.  
  7.  
  8.  
  9. 1. Introduction
  10.  
  11. String Magic is an application which simplifies the maintenance of 
  12. string resources by associating symbolic names with each
  13. string in the resource.  When you add or delete strings,
  14. String Magic automatically updates the symbolic references. 
  15. String Magic supports 'STR#' resources but also implements two
  16. new string resource types:  'STR2' and 'STR3'.  'STR2' is an
  17. indexed string resource.  This means that loading a
  18. particular string from the resource can be much faster than
  19. when using 'STR#' resources.  'STR3' is an indexed string
  20. resource system that requires less application memory.
  21. String Magic can generate Resource files or rez source code files.
  22. Addionally, String Magic always generates a header file containing
  23. #defines's for all of your symbolic names.
  24.  
  25. The header file is named by appending a .h to the source
  26. file name.
  27. The resource is named by appending .rsrc, and
  28. the rez source file is named by appending .r
  29.  
  30.     __________________________________________________________________
  31.     For Example:
  32.     
  33.     Source:     Example.str
  34.     
  35.     Header:        Example.str.h
  36.     Resource:    Example.str.rsrc
  37.     Rez:        Example.str.r
  38.     __________________________________________________________________
  39.  
  40.  
  41.  
  42. 2. User Interface
  43.  
  44. To use String Magic you must first select a source file.  After
  45. selecting a source file, String Magic will pick default names for
  46. the resource and header files. You may change these if you wish.
  47. Simply press the select button next to the appropriate field
  48. and you will be presented with a standard file box.
  49.     
  50. Next choose the resource type that you wish to generate.  There
  51. are three radio buttons from which you may choose 'STR3', 'STR2',
  52. or 'STR#'.
  53.  
  54. If you want to generate source files for the MPW Rez tool, then
  55. click on the Rez check the "Generate Rez Source" checkbox.  If
  56. this box is not checked, then String Magic will create a file
  57. with an empty data fork, and a resource fork containing your
  58. resources.
  59.  
  60. The Append resource check box is useful when you want to add or
  61. replace a string resource in an existing file.
  62.  
  63. Finally click on the "Compile" button to generate the resource and
  64. header file from the selected source file.
  65.  
  66. 3. String Magic Source Format
  67.  
  68. Each source file contains descriptions for one or more string
  69. resources.  The input format is always the same regardless
  70. of which resource type you choose to use.  This allows a lot
  71. of flexibility choosing the right resource type for a specific
  72. application. (See Choosing the right resource type.)
  73.  
  74. Each new resource is introduced by a Resource header consisting
  75. of a percent character (%) followed by the symbolic Resource name
  76. and the Resource number.  Resource attributes may optionally 
  77. follow.
  78.  
  79.     __________________________________________________________________
  80.     # For example to begin a resource called 'HelpStrings' that is a
  81.     # discardable resource you would enter:
  82.     
  83.     %HelpStrings 128 discardable
  84.     __________________________________________________________________
  85.  
  86. The resource number may be any number specified in decimal or
  87. hexidecimal (precede hex numbers with 0x).  For 'STR3' resources
  88. the resource number MUST be in the range 128 - 143.
  89.  
  90. White space (including end of line characters) is used only to
  91. delimit identifiers and keywords (See Identifiers and Keywords below)
  92. One exception is within quoted strings as discussed below.
  93.  
  94.     __________________________________________________________________
  95.     # Therefore the follow is equivalent to the previous example:
  96.     
  97.     %
  98.     HelpStrings 128
  99.                 discardable
  100.     __________________________________________________________________
  101.  
  102.  
  103. After the Resource header comes the string definitions.  A string
  104. definition consists of a String Name and a String value. 
  105.  
  106.     __________________________________________________________________
  107.     # This example shows two resource definitions:
  108.     
  109.     %
  110.     HelpStrings  128 preload
  111.     AboutMessage    "Wizbang is the coolest new Mac App around"
  112.     FileMenuMessage    "The File Menu allows you to Open Wizbang documents"
  113.     EditMenuMessage    "The Edit Menu allows you to correct mistakes"
  114.     
  115.     %
  116.     ButtonStrings 129 
  117.     sOK "OK" sCancel "Cancel" sDefaults "Defaults"
  118.     
  119.     __________________________________________________________________
  120.  
  121.  
  122.  
  123. Identifiers and Keywords
  124.  
  125. Identifiers are used to symbolically represent Resource Names, and
  126. Names.  Identifiers follow the same rules as for C programs.  They
  127. may contain Letters, digits, and underscore characters.  They may
  128. not begin with a digit.  Identifiers should not exceed 63 
  129. characters.
  130.  
  131. The following keywords are recognized by String Magic.  They
  132. represent resource attributes similar to those accepted
  133. by rez.  The keywords are case sensitive.  See Inside Mac Vol I
  134. for a descripion of resource attributes.
  135.  
  136.     appHeap            (default)
  137.     sysHeap            
  138.     
  139.     nonPurgeable    (default)
  140.     purgeable        
  141.     
  142.     unlocked        (default)
  143.     locked            
  144.     
  145.     unprotected        (default)
  146.     protected
  147.     
  148.     nonPreload        (default)
  149.     preload
  150.     
  151.     
  152.     changed            (Ignored)
  153.     unchanged        (Ignored)
  154.     
  155. If two conflicting attributes are specified, the one specified last
  156. takes precedence.
  157.  
  158. Strings
  159.  
  160. String Magic is very flexible in how it accepts string values.  The only 
  161. rule is that the string MUST be enclosed within double quote (")
  162. characters.  The string may span more than one line.  In this case
  163. the new line character IS part of the resulting string.  This makes
  164. it conventent when entering multi-line strings because you can 
  165. enter the string exactly the way you want it to be seen.
  166.  
  167. Additionally the following characters sequences are special
  168. when they appear within string values:
  169.     \t    - Generates a horizontal tab         (ascii 9)
  170.     \b    - Generates a backspace character     (ascii 8)
  171.     \r    - Generates a linefeed                 (ascii 10)
  172.     \n    - Generates a carriage return         (ascii 13)
  173.     \f    - Generates a form feed             (ascii 12)
  174.     \v    - Generates a vertical tab             (ascii 11)
  175.     \?    - Generates an ascii del character     (ascii 127)
  176.     
  177.     \\    - Generates a backslash
  178.     \"    - Generates a double quote
  179.     \'    - Generates a single quote - Not necessary but useful for
  180.                                      compatibility with other languages
  181.                                      
  182.     \$??                 - Ascii hex value
  183.     \0x??? or \0X???    - Ascii hex value
  184.     \0d??? or \0D???    - Ascii decimal value
  185.     \???                - Ascii octal value
  186.     \b???????? or
  187.     \B????????            - Ascii binary value
  188.     
  189.     \  followed by a new line continues the line similar
  190.        to the C language.
  191.     
  192.     __________________________________________________________________
  193.     # The three strings in the following resource are equivalent:
  194.     (Assume that the left edge of this example is column 1)
  195.     
  196.     % TwoLineStrings 129
  197.     
  198.     # Uses \n to enter a carriage return
  199.     string1 "This is a\ntwo line string"
  200.     
  201.     # Uses a real carriage return.
  202.     string2 "\
  203.     This is a
  204.     two line string"
  205.     
  206.     # Uses a hexidecimal ascii value to enter a carriage return
  207.     string3 "This is a\0x0Dtwo line string"
  208.     __________________________________________________________________
  209.  
  210. Comments
  211. A comment is delimited by a hash (#) and the end of the line.  
  212. A comment may appear any where in a source file (except within
  213. a quoted string.) 
  214.  
  215. Operators
  216.  
  217. There are only two operators recognized by String Magic.  The first is the
  218. New Resource operator (%) which introduces a new Resource.
  219.  
  220. The second operator is the New Block operator (+) which is used to
  221. group strings in 'STR3' resources.  The New Block operator is ignored
  222. when generating 'STR2' and 'STR#' resources.  See the description
  223. of 'STR3' resources for more information about the (+) operator.
  224.  
  225. 4. Choosing the right resource type.
  226.  
  227. 'STR#' RESOURCES
  228. ----------------
  229. The standard indexed string resource 'STR#' has two pitfalls
  230. if you need access to a large number of strings.  First of all 
  231. each time you load a string with GetIndString(), ALL of the 
  232. strings in the resource must be loaded into memory.  Second,
  233. the routine must to a linear search to locate the specific string
  234. that you want.  If your resource contains several hundred strings,
  235. then accessing those toward the end of the list can be real slow.
  236.  
  237. 'STR#' resources should only be use when you have a very small number
  238. of strings (less than 20) to store in the resource.
  239.  
  240. 'STR2' RESOURCES
  241. ----------------
  242. 'STR2' resources are similar to 'STR#' resources in that when you load
  243. a specific string, you must load all of the strings in the resource
  244. into memory.  However, 'STR2' resources also contain an index which
  245. allows accessing of the string to be accomplished in much less time.
  246. At the front of the resource is a table which contains an index to
  247. every 16 string block in the resource.  Therefore  accessing a
  248. single string takes at most 17 index computations.  One to get the
  249. block index, and 16 (worst case) to get the string within the block.
  250. Compare that to the 300 or more index computations needed for a large
  251. 'STR#' resource.
  252.  
  253. 'STR3' RESOURCES
  254. ----------------
  255. 'STR3' resources address both pitfalls discussed above.  A 'STR3' 
  256. resource is actually a collection of resources.  Each 'STR3'
  257. resource contains at most 16 strings.  Therefore when you need to
  258. load a specific string, the resource manager never loads more than
  259. 16 strings into memory at one time.  This also means that for any
  260. string, no more than 16 index computations are ever needed.
  261.  
  262. When you define a 'STR3' resource in a String Magic source file, String Magic
  263. actually creates a separate resource for each block of 16 strings.
  264.  
  265. The resource ID that you specify for a 'STR3' resource is actually
  266. a Master ID.  String Magic uses this Master ID to generate the real
  267. resources ID's for each 'STR3' resource.  
  268.  
  269. To generate the real resource ID the following scheme is used:
  270. First we first subtract 128 from the Master ID. This gives
  271. us a value between 0 and 15.  (Remember that resource number for
  272. 'STR3' resources must be in the range 128 - 143).
  273. The above value is placed in the upper 4 bits of the real resource ID.
  274. Then we put the block number int the remaining 12 bits.
  275.  
  276.     __________________________________________________________________
  277.     # For example:
  278.     %
  279.     MyStrings 128
  280.     s1 "aaa" s2 "bbb" s3 "ccc" s4 "ddd" s5 "eee" s6 "fff" 
  281.     s7 "ggg" s8 "hhh" s9 "iii" s10 "jjj" s11 "kkk" s12 "lll" 
  282.     s13 "mmm" s14 "nnn" s15 "ooo" s16 "ppp" s17 "qqq" s18 "rrr" 
  283.     
  284.     # Would generate two 'STR3' Resources:
  285.     Resource 1 would contain:
  286.         "aaa" "bbb" "ccc" "ddd" "eee" "fff" "ggg" "hhh" "iii"
  287.         "jjj" "kkk" "lll" "mmm" "nnn" "ooo" "ppp"
  288.         
  289.     Resource 2 would contain:
  290.         "qqq" "rrr"
  291.     __________________________________________________________________
  292.  
  293. The first actual resource for the 'STR3' resource specified as
  294. resource number 129 in the input file would have the real ID
  295. number of: 0x1001 or 4097
  296.  
  297. To get a better feel for how this works, create a small source file
  298. and compile it with the -src option. 
  299.  
  300.     String Magic -src test.str
  301.     
  302. This will generate a commented rez source file.
  303.  
  304. When creating 'STR3' resources it is sometimes convenient to group
  305. strings that will be used together into the same block.  This way
  306. they will all be loaded into memory by the resource manager at the
  307. same time.  To acomplish this you can use the New Block 
  308. operator (+).  When String Magic Encounters a + in the input file,
  309. it will start a new block (ie. resource). This way you can
  310. be assured that the next 16 strings will all be in the same
  311. resource.  If the (+) operator occurs in a position where a new
  312. block would have been started anyway then it has no effect.  In other
  313. words, it will not cause a block to be skipped.
  314.  
  315. 5.  Loading Strings.
  316.  
  317. To load a string from a 'STR#' resource you use the Toolbox routine
  318. GetIndString.  The prototype looks like this:
  319. pascal void GetIndString(Str255 buf, short ResID, short index);
  320.  
  321. For ResID you simply use the name of the resource as specifed in
  322. the String Magic source file. And for the index you use the String Name.
  323.  
  324. Similarly for 'STR2' and 'STR3' resources you would use:
  325. GetIndStr2() and GetIndStr3() respectively.  These routines take
  326. the same parameters as GetIndString.
  327.  
  328. To access the GetIndStr2() and GetIndStr3() routines you
  329. must link you program with FastStrings.c.o.  I suggest putting
  330. this file in your {CLibraries} directory.
  331.  
  332. 6. Use of String Magic and supporting routines.
  333.  
  334.             String Magic V1.0 is FREE. 
  335.             
  336. You may use String Magic and the supporting routines
  337. free of charge.  
  338.  
  339. If you have any Questions or Comments you can contact me at:
  340.  
  341. America Online: Screen Name "CurtS5"
  342.  
  343. I'd like to know whether you found String Magic useful or not.
  344. Help keep the dust out of my AO mailbox, send me some mail!
  345.  
  346. or
  347.  
  348. Curt Sellmer
  349. 11028 Jollyville Road #342
  350. Austin, TX  78759
  351.  
  352.